<template>
    <div class="content-page">
        <div class="content-nav">
            <el-breadcrumb class="breadcrumb" separator="/">
                <el-breadcrumb-item :to="{ name: 'dashboard' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>商品属性管理</el-breadcrumb-item>
                <el-breadcrumb-item>{{infoForm.id ? '编辑属性' : '添加属性'}}</el-breadcrumb-item>
            </el-breadcrumb>
            <div class="operation-nav">
                <el-button type="primary" @click="goBackPage" icon="arrow-left">返回列表</el-button>
            </div>
        </div>
        <div class="content-main">
            <div class="form-table-box">
                <el-form ref="infoForm" :rules="infoRules" :model="infoForm" label-width="120px">
                    <el-form-item label="属性分类" prop="attribute_category_id">
                        <el-select v-model="infoForm.attribute_category_id" placeholder="请选择属性分类">
                            <el-option v-for="item in infoForm.attributecategory" :key="item.id" :label="item.name"
                                       :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="属性名称" prop="name">
                        <el-input v-model="infoForm.name"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmitInfo">确定保存</el-button>
                        <el-button @click="goBackPage">取消</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
    import api from '@/config/api';

    export default {
        name: "AttributeAddPage",
        data() {
            return {
                uploaderHeader: {
                    'x-lingang-token': localStorage.getItem('token') || '',
                },
                infoForm: {
                    id: 0,
                    name: "",
                    attributecategory: [{
                        id: 0,
                        name: '全部'
                    }],
                    attribute_category_id: 0,
                },
                infoRules: {
                    name: [
                        {required: true, message: '请输入名称', trigger: 'blur'},
                    ],
                    attribute_category_id: [
                        {required: true, min: 1, message: '请选择分类', trigger: 'blur'},
                    ]
                },
            }
        },
        methods: {
            goBackPage() {
                this.$router.go(-1);
            },
            onSubmitInfo() {
                this.$refs['infoForm'].validate((valid) => {
                    if (valid) {
                        this.axios.post('attribute/store', this.infoForm).then((response) => {
                            if (response.data.errno === 0) {
                                this.$message({
                                    type: 'success',
                                    message: '保存成功'
                                });
                                this.$router.go(-1)
                            } else {
                                this.$message({
                                    type: 'error',
                                    message: '保存失败'
                                })
                            }
                        })
                    } else {
                        return false;
                    }
                });
            },
            getAttributeCategory() {
                this.axios.get('attributecategory/list').then((response) => {
                    this.infoForm.attributecategory = response.data.data;
                })
            },
            getInfo() {
                if (this.infoForm.id <= 0) {
                    return false
                }

                //加载分类详情
                let that = this
                this.axios.get('attribute/info', {
                    params: {
                        id: that.infoForm.id
                    }
                }).then((response) => {
                    let resInfo = response.data.data;
                    that.infoForm = resInfo;
                })
            }

        },
        components: {},
        mounted() {
            this.infoForm.id = this.$route.query.id || 0;
            this.getInfo();
            this.getAttributeCategory();
        }
    }

</script>
